package com.jiazhong.mingxing.boot.boot11.过滤消息;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

@Slf4j
public class 过滤消息生产 {

    private static final String[] citys = {"西安", "宝鸡", "咸阳", "榆林", "商洛"};

    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("jiazhong-mingxing");
        producer.setNamesrvAddr("124.220.170.254:9876");
        producer.start();
        for (int i = 1; i < 101; i++) {
            char gender = ((int) (Math.random() * 100 + 1)) % 2 == 0 ? '男' : '女';
            int age = (int) (Math.random() * 100 + 1);
            String city = citys[(int) (Math.random() * citys.length)];
            String topic = "TopicE";
            String tags = "guolv";
            String body = "Hello,Guolv!City:" + city + ",gender:" + gender + ",age:" + age;
            Message message = new Message(topic, tags, body.getBytes(StandardCharsets.UTF_8));
            // 设置属性
            message.putUserProperty("city", city);
            message.putUserProperty("age", age + "");
            message.putUserProperty("gender", gender + "");
            SendResult sendResult = producer.send(message);
            log.info("sendResult={}", sendResult);
        }
        producer.shutdown();
    }
}
